<?php

use eolinker\command\Eolinker;
use think\Console;

$app = app();

if ($app->request->isCli()) {
    // 动态注册操作指令
    Console::starting(function (Console $console) {
        $console->addCommand(Eolinker::class);
        $console->addCommand(\app\data\command\Test::class);
        $console->addCommand(\app\data\command\SyncCarInfo::class);
        $console->addCommand(\app\data\command\WechatMessageOrcImage::class);
        $console->addCommand(\app\data\command\MessageParse::class);
        $console->addCommand(\app\data\command\ReportObservation::class);
        $console->addCommand(\app\data\command\ReportDailyOutput::class);
        $console->addCommand(\app\data\command\NotCompleteRecordNotify::class);
        $console->addCommand(\app\data\command\NotReplyRecordNotify::class);
        $console->addCommand(\app\data\command\RecordRemarkRemind::class);
        $console->addCommand(\app\data\command\NotCompleteRecordAgain::class);
    });
} else {
    // 注册订单确认支付事件
    $app->event->listen('ShopOrderConfirm', function ($orderNo) use ($app) {

    });
}

if (!function_exists('getHttpHost')) {

    function getHttpHost($getFullUrl = false)
    {
        $url = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
        $url .= "://".$_SERVER['HTTP_HOST'];
        if ($getFullUrl) {
            $url .= $_SERVER['REQUEST_URI'];
        }
        return $url;
    }
}

if (!function_exists('format_time')) {

    function format_time(string $time, string $format = 'Y-m-d H:i'): string
    {
        return date($format, strtotime($time));
    }
}

if (!function_exists('format_date')) {

    function format_date($time, string $format = 'Y-m-d'): string
    {
        if (empty($time)) {
            return '';
        }
        return date($format, strtotime($time));
    }
}

if (!function_exists('formatContent')) {

    function formatContent($content)
    {
        if (!is_string($content)) {
            return $content;
        }
        $content = str_replace('max-width:750px', 'max-width:100%', $content);
        $content = preg_replace(
            array(
                '/(<img [^<>]*?)width=.+?[\'|\"]/',
                '/(<img.*?)((height)=[\'"]+[0-9|%]+[\'"]+)/',
                '/(<img.*?)((style)=[\'"]+(.*?)+[\'"]+)/',
            ) , array('$1 max-width="100%" ', '$1', '$1') , $content);
        return str_replace('<img ', '<img style="max-width:100%;vertical-align:bottom" ', $content);
    }
}

if (!function_exists('hiddenPhone')) {

    function hiddenPhone($phone)
    {
        return substr_replace($phone,'****',3,4);
    }
}

if (!function_exists('formatPrice')) {

    function formatPrice($price, $trimZero = false)
    {
        if ($trimZero) {
            return (string)floatval(substr(sprintf("%.3f", $price), 0, -1));
        }
        return substr(sprintf("%.3f", $price), 0, -1);
    }
}

if (!function_exists('getDistance')) {

    /**
     * 求两个已知经纬度之间的距离,单位为米
     * @param $lng1
     * @param $lat1
     * @param $lng2
     * @param $lat2
     * @return float|int
     */
    function getDistance($lng1, $lat1, $lng2, $lat2)
    {
        if ((!$lng1 && !$lat1) || (!$lng2 && !$lat2) ) {
            $s = '';
        } else {
            // 将角度转为狐度
            $radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
            $radLat2 = deg2rad($lat2);
            $radLng1 = deg2rad($lng1);
            $radLng2 = deg2rad($lng2);
            $a = $radLat1 - $radLat2;
            $b = $radLng1 - $radLng2;
            $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
        }
        return $s;
    }
}
